<?php 

namespace Home\Controller;
use Hdphp\Library\Controller;

/**
*  提问
*/
class AskController extends CommonController
{
	//这种写法出错
/*	public function __construct(){
		// parents::__construct();
		// if( !isset($_SESSION['username']) || !isset($_SESSION['uid']) ){
		// 	$this->error('请先登录后提问');
		// }
	}	*/

	public function index(){

		//导航分类
		$this->topNav();

		//提问分类
		$sql="SELECT * FROM hd_category WHERE pid=0 ";
		$category=M()->query($sql);
		$this->assign('category',$category);

		//悬赏金币
		if(isset($_SESSION['uid'])){
			$sql="SELECT * FROM hd_user WHERE uid={$_SESSION['uid']}";
			$userPoint=M()->query($sql);
			$this->assign('userPoint',$userPoint);
		}
		$this->display('ask.html');
	}

	//ajax 读取分类
	public function ajaxCate(){
		if(!IS_AJAX) $this->error('非法访问','./index.php?c=ask');
		$cid=intval($_POST['cid']);
		$sql="SELECT * FROM hd_category WHERE pid={$cid} ";
		$category=M()->query($sql);
		if($category){
			echo json_encode($category);die;
		}else{
			echo 0;
			$this->error('数据出错');die;
		}

	}


	//ajax 检测用户名
	public function checkUser(){
		if(!IS_AJAX) $this->error('非法访问');
		//检测用户是否登录
		$info=array();
		if( !isset($_SESSION['username']) || !isset($_SESSION['uid']) ){
			$info['status']=0;
			$info['msg']='请先登录后提问';
			echo json_encode($info);die;
			// $this->error('请先登录后提问');
		}

		$point=intval($_POST['point']);

		$sql="SELECT * FROM hd_user WHERE uid={$_SESSION['uid']} AND point>={$point}";

		if( M()->query($sql) ){
			$info['status']=1;
			echo json_encode($info);die;
		}else{
			$info['status']=0;
			$info['msg']='金币不足';
			echo json_encode($info);die;
			// $this->error('金币不足');
		}

	}

	//添加提问问题
	public function addAsk(){
		if(!IS_POST) $this->error('错误访问');
		if( !isset($_SESSION['username']) && !isset($_SESSION['uid']) ){
			$this->error('请登录后发布问题');
		}

		$cid=intval($_POST['cid']);
		$content=htmlspecialchars($_POST['content']);
		$reward=intval($_POST['reward']);
		$uid=$_SESSION['uid'];
		$time=time();

		$sql="INSERT INTO hd_ask SET content='{$content}',time={$time},reward={$reward},uid={$uid},cid={$cid} ";

		if(M()->exec($sql)){
			$this->success('发布成功','./index.php?c=ask');
		}else{
			$this->erro('发布失败');
		}

	}


}


 ?>